package com.example.javademo;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class DialogActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_dialog);

        // 隐藏 ActionBar
        if (getSupportActionBar() != null) {
            getSupportActionBar().hide();
        }
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
    }

//    普通对话框
    public void clickDialog(View view){
//        Button button = findViewById(R.id.dialog_button);
        if(view.getId() == R.id.dialog_button){
//
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("提示");
            builder.setMessage("这是一个普通的对话框");
            builder.setPositiveButton("确定", (dialog, which) -> {
                // 用户点击确定按钮后的操作
//                dialog.dismiss();
                finish();// 击“确定”后，关闭当前 Activity
            });
            builder.setNegativeButton("取消", (dialog, which) -> {
                // 用户点击取消按钮后的操作
                dialog.dismiss(); // 关闭对话框
            });
            builder.show();
//            AlertDialog dialog = builder.create();
//            dialog.show();

//             第二种方式
//            clickDialog2(view);
        } else if (view.getId() == R.id.dialog_button2) {
            TestDialog dialog = new TestDialog(this, R.style.TestDialog);
            dialog.show();
        }
    }

//    设置自定义对话框
    public void clickDialog2(View view){
        AlertDialog dialog = new AlertDialog.Builder(this).create();
        dialog.setTitle("自定义对话框");
        dialog.setMessage("这是一个自定义对话框");
        /*传统方式*/
        dialog.setButton(AlertDialog.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        /*dialog.setButton(AlertDialog.BUTTON_POSITIVE, "确定", (dialog1, which) -> {
            dialog1.dismiss();
        });*/
        dialog.show();

    }

}